package com.github.cosycode.imgtool.action;

import com.github.cosycode.common.ext.bean.DoubleBean;
import com.github.cosycode.common.util.otr.PrintTool;
import com.github.cosycode.flow.base.AbstractAction;
import com.github.cosycode.imgtool.ctrl.MarkdownImgLinkMatchAnalyzer;
import com.github.cosycode.imgtool.rule.CheckConfig;
import com.github.cosycode.imgtool.rule.MarkdownRepairSource;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * <b>Description : </b>
 * <p>
 * <b>created in </b> 2023/2/4
 * </p>
 *
 * @author CPF
 * @since
 **/
@AllArgsConstructor
public class AddressMdLinkByModifyLinkAction extends AbstractAction<AddressMdLinkByModifyLinkAction.MdLlMfReport> {

    private MarkdownRepairSource source;
    private CheckConfig config;

    public static void main(String[] args) throws Exception {
        // 1. 数据源
        String projectPath = "P:\\git-note\\working-note";

        MarkdownRepairSource source = new MarkdownRepairSource();
        source.addMarkdownFile(new File(projectPath));
        source.addImgFile(new File(projectPath));

        boolean dryRun = true;
        CheckConfig config = new CheckConfig();
//        config.setWebPathEnable(true, false);
        config.setRelatePathEnable(true);
//        config.setAbsolutePathEnable(true);
        config.setRootPathEnable(projectPath);

        AddressMdLinkByModifyLinkAction addressMdLinkByModifyLinkAction = new AddressMdLinkByModifyLinkAction(source, config);
        addressMdLinkByModifyLinkAction.setDryRun(dryRun);
        addressMdLinkByModifyLinkAction.doProcess();
    }

    @Override
    public AddressMdLinkByModifyLinkAction.MdLlMfReport doProcess() throws Exception {
        MarkdownImgLinkMatchAnalyzer.IMdImgLinkModifyHandle stringBiFunction =
                MarkdownImgLinkMatchAnalyzer.IMdImgLinkModifyHandle.geneRelativePathHandle(".img");
        Map<String, List<DoubleBean<String, String>>> listMap = MarkdownImgLinkMatchAnalyzer.execMdImgLinkMatch(
                source.getMarkdownFileSet(), source.getPicFileSet(), config,
                stringBiFunction, dryRun);
        MdLlMfReport report = new MdLlMfReport();
        report.setListMap(listMap);
        return report;
    }

    @Data
    public static class MdLlMfReport {
        Map<String, List<DoubleBean<String, String>>> listMap;

        public void printChangeImgLinkList() {
            for (Map.Entry<String, List<DoubleBean<String, String>>> entry : listMap.entrySet()) {
                PrintTool.info("file ==> {}", entry.getKey());
                List<DoubleBean<String, String>> value = entry.getValue();
                for (DoubleBean<String, String> doubleBean : value) {
                    PrintTool.info("    l ||| {} -> {}", doubleBean.getO1(), doubleBean.getO2());
                }
            }
        }
    }


}
